package com.flowerplus.store.common.structure.ui.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.flowerplus.store.App;
import com.flowerplus.store.Logger;
import com.flowerplus.store.common.data.injector.component.ApplicationComponent;
import com.flowerplus.store.common.data.injector.module.ActivityModule;
import com.flowerplus.store.common.data.utils.SpUtil;
import com.flowerplus.store.common.structure.key.TokenKey;
import com.flowerplus.store.common.structure.mvp.BasePresenter;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by ems on 2017/2/27.
 * activity基类
 */

public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";
    protected BaseActivity mActivity;
    protected BasePresenter mBasePresenter;
    protected Unbinder mBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Logger.v(TAG, " onCreate");
        setContentViewBefore();
        setContentView(layoutResId());
        if (isStatusBarTransparent()) {
            setStatusBarTransparent();
        }
        mActivity = this;
        initStatus();
        mBind = ButterKnife.bind(this);
        initInject();
        initViews();
        initData();
        initEvents();
        mBasePresenter = getPresenter();

    }

    protected void setContentViewBefore() {


    }


    /**
     * 设置透明状态栏
     */
    private void setStatusBarTransparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    protected boolean isStatusBarTransparent() {
        return false;
    }

    protected abstract void initViews();


    protected abstract void initData();


    protected abstract void initEvents();


    public ActivityModule getActivityModule() {
        return new ActivityModule(this);

    }

    public ApplicationComponent getApplicationComponent() {
        return App.getApp().getApplicationComponent();
    }

    protected void initStatus() {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (mBasePresenter != null) {
            mBasePresenter.onDestroy();
            mBasePresenter = null;
        }
        if (mBind != null) {
            mBind.unbind();
            mBind = null;
        }
        super.onDestroy();
    }

    //内存变化时调用
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Logger.d("onTrimMemory..." + level);
    }

    public abstract void initInject();

    protected abstract int layoutResId();

    protected abstract BasePresenter getPresenter();
    protected boolean hasLogin(){
        return !TextUtils.isEmpty(SpUtil.getInstance().getString(TokenKey.USER_ID));
    }
}
